package com.dibang.app.mvp.dialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.annimon.stream.Stream;
import com.dibang.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxl on 2019/5/23.
 */

public class LoadingDialogActivity extends Activity {

    private static List<LoadingDialogActivity> dialogs = new ArrayList<>();

    private boolean cancelTouchOutSide;

    private boolean backPressCancel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dailog_loading);
        dialogs.add(this);
        TextView msgTextView = findViewById(R.id.loading_dialog_text);
        String msg = getIntent().getStringExtra("msg");
        if (!TextUtils.isEmpty(msg)) {
            msgTextView.setText(msg);
        }
        boolean isShowMsgView = getIntent().getBooleanExtra("showMsgView", true);
        msgTextView.setVisibility(isShowMsgView ? View.VISIBLE : View.GONE);
        cancelTouchOutSide = getIntent().getBooleanExtra("cancelTouchOutSide", true);
        backPressCancel = getIntent().getBooleanExtra("backPressCancel", true);
        findViewById(R.id.dialog_loading_view)
                .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cancelTouchOutSide){
                    finish();
                }
            }
        });
    }


    /**
     * 显示加载中对话框
     * @param cxt
     * @param msg
     * @param isShowMsg
     * @param cancelTouchOutSide
     * @param backPressCancel
     */
    public static void show(Context cxt, String msg, boolean isShowMsg, boolean cancelTouchOutSide, boolean backPressCancel) {
        if(cxt == null) return;
        Intent savedInstanceState = new Intent(cxt, LoadingDialogActivity.class);
        savedInstanceState.putExtra("msg", msg);
        savedInstanceState.putExtra("showMsgView", isShowMsg);
        savedInstanceState.putExtra("cancelTouchOutSide", cancelTouchOutSide);
        savedInstanceState.putExtra("backPressCancel", backPressCancel);
        savedInstanceState.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        cxt.startActivity(savedInstanceState);
    }


    /**
     * 显示默认加载中对话框
     * @param cxt
     */
    public static void show(Context cxt){
        if(cxt == null) return;
        Intent savedInstanceState = new Intent(cxt, LoadingDialogActivity.class);
        savedInstanceState.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        cxt.startActivity(savedInstanceState);
    }

    /**
     * 关闭对话框
     */
    public static void dismiss(){
        if(!dialogs.isEmpty()){
            Stream.of(dialogs).forEach(loadingDialogActivity -> loadingDialogActivity.finish());
        }
    }

    @Override
    public void onBackPressed() {
        if(backPressCancel) {
            super.onBackPressed();
        }
    }
}
